package com.qianguo.shop.fragment.tab;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.qianguo.shop.R;
import com.qianguo.shop.activity.OrderDetailsActivity;
import com.qianguo.shop.base.BaseFragment;
import com.qianguo.shop.fragment.DoingOrderFragment_;
import com.qianguo.shop.fragment.DoneOrderFragment_;
import com.qianguo.shop.fragment.TodoOrderFragment_;

import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 商家订单tab
 *
 * @author GUXIA318
 * @date 2015-09-14 17:28
 */
@EFragment(R.layout.fragment_tab_business_order)
public class OrderTabFragment extends BaseFragment {

    private static final int STATUS_TODO = 0;
    private static final int STATUS_DOING = 1;
    private static final int STATUS_DONE = 2;

    public static String[] ORDER_STATUSES;

    @ViewById(R.id.order_tab_layout)
    TabLayout mTabLayout;
    @ViewById(R.id.order_view_pager)
    ViewPager mViewPager;

    private OrderFragmentPagerAdapter mAdapter;

    @Override
    protected void initializeUI() {
        ORDER_STATUSES = getResources().getStringArray(R.array.order_statuses);

        mAdapter = new OrderFragmentPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
    }

    public class OrderFragmentPagerAdapter extends FragmentPagerAdapter {

        public OrderFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f = null;
            switch (position) {
                case STATUS_TODO:
                    f = new TodoOrderFragment_();
                    break;
                case STATUS_DOING:
                    f = new DoingOrderFragment_();
                    break;
                case STATUS_DONE:
                    f = new DoneOrderFragment_();
                    break;
            }

            Bundle extras = new Bundle();
            extras.putInt("position", position);
            f.setArguments(extras);
            return f;
        }

        @Override
        public int getCount() {
            return ORDER_STATUSES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return ORDER_STATUSES[position];
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OrderDetailsActivity.ORDER_DETAILS_REQUEST) {
            if (resultCode == OrderDetailsActivity.WEIGHT_FINISH) {
                if (mViewPager.getCurrentItem() < STATUS_DOING) {
                    mViewPager.setCurrentItem(STATUS_DOING);
                }
            }
        }
    }
}
